﻿
Imports std
Imports xmlctrl
Imports webctrl
Imports elpmis
Imports lpmsengi
Imports avacom

Public Class preview_pagination
    Inherits nameLpmsControl

    Private Sub New(ByVal page As xhtmlpage, ByVal parameters As parameters_t)
        MyBase.New(page, "pagination", parameters:=parameters)
    End Sub

    Public Shared Function create(ByVal context As httpContext, ByVal page As xhtmlpage, _
                                  ByVal lastlink As String, ByVal nextlink As String) As xmlcontrol
        Dim parameters As parameters_t = Nothing
        parameters = New parameters_t()
        parameters.push_back(make_pair("lastlink", _
                                       convertor.cast(Of bare_variable)( _
                                           New const_bare_variable(typeDefination.string, lastlink))))
        parameters.push_back(make_pair("nextlink", _
                                       convertor.cast(Of bare_variable)( _
                                           New const_bare_variable(typeDefination.string, nextlink))))
        Return context.createControl(New preview_pagination(page, parameters))
    End Function
End Class

Public Class preview_timeline
    Inherits nameLpmsControl

    Private Sub New(ByVal page As xhtmlpage, ByVal parameters As parameters_t)
        MyBase.New(page, "preview_timeline", parameters:=parameters)
    End Sub

    Public Shared Function create(ByVal context As httpContext, ByVal page As xhtmlpage, _
                                  ByVal startticks As Int64) As xmlcontrol
        Dim parameters As parameters_t = Nothing
        parameters = New parameters_t()
        parameters.insert("dateOffset", typeDefination.int, data.timeline.index.dateOffset)
        parameters.insert("weekOffset", typeDefination.int, data.timeline.index.weekOffset)
        parameters.insert("monthOffset", typeDefination.int, data.timeline.index.monthOffset)
        parameters.insert("yearOffset", typeDefination.int, data.timeline.index.yearOffset)
        parameters.insert("urlprefix", typeDefination.string, _
                          strmid(timelinepvp._pathidentity, strlen(network.pathSeperator)))
        parameters.insert("startticks", typeDefination.int, startticks)
        Return context.createControl(New preview_timeline(page, parameters))
    End Function
End Class

Public Class quickcomment
    Inherits nameLpmsControl

    Private Sub New(ByVal page As xhtmlpage, ByVal parameters As parameters_t)
        MyBase.New(page, "quickcomment", parameters:=parameters)
    End Sub

    Public Shared Function create(ByVal context As httpContext, _
                                  ByVal page As xhtmlpage, _
                                  ByVal cs As String) As xmlcontrol
        Dim parameters As parameters_t = Nothing
        parameters = New parameters_t()
        parameters.push_back(make_pair(query.checksum, _
                                       convertor.cast(Of bare_variable)( _
                                           New const_bare_variable(typeDefination.string, cs))))
        Dim s As String = Nothing
        If getIdFromContext(context, s) Then
            parameters.push_back(make_pair("userid", _
                                           convertor.cast(Of bare_variable)( _
                                               New const_bare_variable(typeDefination.string, s))))
        End If
        Dim ava As avatar = Nothing
        ava = context.createControl(New avatar())
        ava.setData(s)
        parameters.insert("avatar", typeDefination.string, Convert.ToString(ava))
        parameters.push_back(make_pair("LOC_WRITER_ID", _
                                       convertor.cast(Of bare_variable)( _
                                           New const_bare_variable(typeDefination.int, _
                                                                   convertor.cast(Of Int64)(stringId.WRITER_ID)))))
        parameters.push_back(make_pair("LOC_WRITER_SUBMIT", _
                                       convertor.cast(Of bare_variable)( _
                                           New const_bare_variable(typeDefination.int, _
                                                                   convertor.cast(Of Int64)(stringId.WRITER_SUBMIT)))))
        parameters.push_back(make_pair("query.id", _
                                      convertor.cast(Of bare_variable)( _
                                          New const_bare_variable(typeDefination.string, query.id))))
        parameters.push_back(make_pair("query.key", _
                                       convertor.cast(Of bare_variable)( _
                                           New const_bare_variable(typeDefination.string, query.key))))
        parameters.push_back(make_pair("query.text", _
                                       convertor.cast(Of bare_variable)( _
                                           New const_bare_variable(typeDefination.string, query.text))))
        parameters.push_back(make_pair("query.cs", _
                                       convertor.cast(Of bare_variable)( _
                                           New const_bare_variable(typeDefination.string, query.checksum))))
        parameters.push_back(make_pair("query.toviewpage", _
                                       convertor.cast(Of bare_variable)( _
                                           New const_bare_variable(typeDefination.string, query.toviewpage))))
        Return context.createControl(New quickcomment(page, parameters))
    End Function
End Class

Public Class sitemapContent
    Inherits nameLpmsControl

    Private Sub New(ByVal parameters As parameters_t)
        MyBase.New("sitemap", parameters:=parameters)
    End Sub

    Public Shared Function create(ByVal context As httpContext) As xmlcontrol
        Dim parameters As parameters_t = Nothing
        parameters = New parameters_t()
        Dim urlprefix As String = Nothing
        urlprefix = strleft(context.url().full(), _
                            strlen(context.url().full()) - _
                            strlen(sitemap._pathidentity)) + _
                    view._pathidentity
        urlprefix = urlUtils.appendQuery(urlprefix, query.key, emptyString)
        parameters.push_back( _
            make_pair("urlprefix", _
                      convertor.cast(Of bare_variable)(New const_bare_variable(typeDefination.string, urlprefix))))

        Dim al As data.alllist.record = Nothing
        al = New data.alllist.record()
        If al.read() Then
            Dim v As vector(Of String) = Nothing
            If list2vector(al, v) Then
                parameters.push_back( _
                    make_pair("records", _
                              convertor.cast(Of bare_variable) _
                                            (New const_bare_variable(typeDefination.vector, _
                                                                     typeDefination.string, _
                                                                     New elpmis_vector(Of String)(v)))))
            End If
        End If

        Return context.createControl(New sitemapContent(parameters))
    End Function
End Class

Public Class categories
    Inherits nameLpmsControl

    Private Sub New(ByVal page As xhtmlpage, ByVal parameters As parameters_t)
        MyBase.New(page, "categories", parameters:=parameters)
    End Sub

    Public Shared Function create(ByVal context As httpContext, _
                                  ByVal page As xhtmlpage, _
                                  ByVal rec As vector(Of recentElement)) As xmlcontrol
        Dim parameters As parameters_t = Nothing
        If Not rec Is Nothing AndAlso rec.size() > 0 Then
            Dim m As mapheap(Of String, Int64) = Nothing
            m = New mapheap(Of String, Int64)()
            For i As Int64 = 0 To rec.size() - 1
                m.accumulate(rec(i).name, 1)
            Next
            parameters = New parameters_t()
            parameters.insert("count", typeDefination.int, m.size())
            Dim vs As vector(Of String) = Nothing
            Dim vc As vector(Of Int64) = Nothing
            vs = New vector(Of String)()
            vc = New vector(Of Int64)()
            While Not m.empty()
                Dim s As String = Nothing
                Dim i As String = Nothing
                m.pop_front(s, i)
                vs.push_back(s)
                vc.push_back(i)
            End While
            parameters.push_back(make_pair("categories", _
                                           convertor.cast(Of bare_variable) _
                                                         (New const_bare_variable( _
                                                             typeDefination.vector, _
                                                             typeDefination.string, _
                                                             New elpmis_vector(Of String)(vs)))))
            parameters.push_back(make_pair("categories_count", _
                                           convertor.cast(Of bare_variable) _
                                                         (New const_bare_variable( _
                                                             typeDefination.vector, _
                                                             typeDefination.int, _
                                                             New elpmis_vector(Of Int64)(vc)))))
            parameters.insert("urlprefix", typeDefination.string, _
                              strmid(catpvp._pathidentity, strlen(network.pathSeperator)))
        End If

        Return context.createControl(New categories(page, parameters))
    End Function
End Class

Public Class above_header
    Inherits nameLpmsControl

    Private Shared filelist As vector(Of String) = Nothing
    Private Shared lock As sequentialLock = Nothing

    Private Shared Sub listfilesImpl()
        lock.wait()
        filelist = listfiles("\\hzj-lowend-2\d$\share\BingImages")
        lock.release()
    End Sub

    Shared Sub New()
        lock = New sequentialLock()
        listfilesImpl()
        timer3.addcall(AddressOf listfilesImpl, secondsToMilliseconds(60 * 60 * 24))
    End Sub

    Private Sub New(ByVal page As xhtmlpage, ByVal parameters As parameters_t)
        MyBase.New(page, "above_header", parameters:=parameters)
    End Sub

    Public Shared Function create(ByVal context As httpContext, _
                                  ByVal page As xhtmlpage) As xmlcontrol
        Dim parameters As parameters_t = Nothing
        parameters = New parameters_t()
        Dim fl As vector(Of String) = Nothing
        lock.wait()
        copy(fl, filelist)
        lock.release()
        parameters.push_back(make_pair("files", _
                                       convertor.cast(Of bare_variable)( _
                                           New const_bare_variable(typeDefination.vector, _
                                                                   typeDefination.string, _
                                                                   New elpmis_vector(Of String)(fl)))))
        Return context.createControl(New above_header(page, parameters))
    End Function
End Class
